<template>
  <div style="margin-top: 10px">
    <a-table
        :columns="columns"
        :data-source="page.list"
        :pagination="pagination"
        @change="handleTableChange"
    >
      <template #visit_id="{ text }">
        <span>
          {{ text }}
        </span>
      </template>
      <template #user_name="{ text }">
        <span>
          <smile-outlined/>
          {{ text }}
        </span>
      </template>
      <template #comment="{ text }">
        <span>
          {{ text }}
        </span>
      </template>
      <template #state="{ text }">
        <b>
          <span v-if="text == 0">
            <loading-outlined/>
            待审核
          </span>
          <span v-if="text == 1" style="color: green"> 已审核 </span>
          <span v-if="text == 2" style="color: red"> 已拒绝 </span>
        </b>
      </template>
      <template #action="{ record }">
        <a @click="openPermitExtra(record)" v-if="record.state == 0">通过</a>
        <a-divider type="vertical" v-if="record.state == 0"/>
        <a @click="rejectExtra(record)" v-if="record.state == 0">拒绝</a>
      </template>
    </a-table>
  </div>

  <a-modal v-model:visible="visible" title="修改时间" @ok="insertTimeInfo">
    <a-form-item label="请选择日期">
      <a-date-picker
          v-model:value="freeTimeNeedsTime"
          :disabled-date="disabledDate"
      />
    </a-form-item>
    <a-form-item label="请选择地点">
      <a-select v-model:value="selectTime.place_name" style="width: 120px">
        <a-select-option value="class01">class01</a-select-option>
        <a-select-option value="class02">class02</a-select-option>
      </a-select>

      <a-button style="margin-left: 15px" @click="getTimeInfo">
        <template #icon>
          <SearchOutlined/>
        </template>
        查询
      </a-button>
    </a-form-item>
    <a-divider/>

    <a-table
        :columns="columns2"
        :data-source="page2.list"
        :pagination="pagination2"
        @change="handleTableChange2"
    >
      <template #time_id="{ text }">
        <span>
          {{ text }}
        </span>
      </template>
      <template #start_time="{ text }">
        <span>
          {{ text }}
        </span>
      </template>
      <template #end_time="{ text }">
        <span>
          {{ text }}
        </span>
      </template>
      <template #action="{ record }">
        <a @click="changeTimeInfo(record)">选择</a>
      </template>
    </a-table>
    <a-form-item label="您选择的配置时间为：">
      {{ updateTimeInfo.time_id }}
    </a-form-item>
  </a-modal>
</template>

<script setup>
////////////////////////////////////////////
import {reactive, ref} from "vue";
import {post, setLocalToken} from "@/common";
import {useRouter} from "vue-router";
import {get} from "@/common";
import {message} from "ant-design-vue";
import dayjs from "dayjs";
//import {DownloadOutlined} from "";
////////////////////////////////////////////
const columns = [
  {
    dataIndex: "visit_id", //
    title: "初访ID",
    slots: {
      customRender: "visit_id",
    },
  },
  {
    dataIndex: "user_name", //
    title: "咨询师姓名",
    slots: {
      customRender: "user_name",
    },
  },
  {
    dataIndex: "comment", //
    title: "简述",
    slots: {
      customRender: "comment",
    },
  },
  {
    dataIndex: "state", //
    title: "状态",
    slots: {
      customRender: "state",
    },
  },
  {
    title: "操作",
    key: "action",
    slots: {
      customRender: "action",
    },
  },
];
const columns2 = [
  {
    dataIndex: "time_id", //
    title: "配置时间ID",
    slots: {
      customRender: "time_id",
    },
  },
  {
    dataIndex: "start_time", //
    title: "起始时间",
    slots: {
      customRender: "start_time",
    },
  },
  {
    dataIndex: "end_time", //
    title: "结束时间",
    slots: {
      customRender: "end_time",
    },
  },
  {
    dataIndex: "apply_id", //
    title: "申请id",
    slots: {
      customRender: "apply_id",
    },
  },
  {
    title: "操作",
    key: "action",
    slots: {
      customRender: "action",
    },
  },
];

const page2 = ref({});

const page = ref({});

const postBody = reactive({
  user_id: "",
});

const selectTime = reactive({
  day: "",
  place_name: "",
});

const updateTimeInfo = reactive({
  place_name: "",
  day: "",
  time_id: "",
  visit_id: "",
  apply_id: "",
});
///////////////////////////////////////////
const pagination = reactive({
  current: 1, //当前页
  pageSize: 5, //每页记录数
  total: 0, //总记录数
  pageSizeOptions: ["2", "3", "5", "10", "20", "30", "100"], //每页记录下拉框的选型
  showSizeChanger: true, //是否展示每页记录数下拉框
  showQuickJumper: true, //是否展示快速跳转
  showTotal: (total, range) => {
    //展示总页数等文字说明信息的
    return (
        "共" +
        total +
        "条记录,当前第" +
        pagination.current +
        "页" +
        (range[1] - range[0] + 1) +
        "条记录"
    );
  },
});

const getPage = (pageNum = 1) => {
  postBody.pageNum = pageNum;
  postBody.pageSize = pagination.pageSize;

  post("/admin/extraManage/getAdditionalApplyInfo", postBody).then((res) => {
    page.value = res.data; //将服务端传来的分页数据赋值予page

    //更新分页引擎
    pagination.current = page.value.current;
    pagination.pageSize = page.value.pageSize;
    pagination.total = page.value.total;

    console.log(res.data.list);
  });
};

getPage(1);

const handleTableChange = (pagi, filters, sorter, {currentDataSource}) => {
  pagination.current = pagi.current;
  pagination.pageSize = pagi.pageSize;
  pagination.total = pagi.total;

  getPage(pagination.current);
};
////////////////////////////////////////////
const pagination2 = reactive({
  current: 1, //当前页
  pageSize: 5, //每页记录数
  total: 0, //总记录数
  pageSizeOptions: ["2", "3", "5", "10", "20", "30", "100"], //每页记录下拉框的选型
  showSizeChanger: true, //是否展示每页记录数下拉框
  showQuickJumper: true, //是否展示快速跳转
  showTotal: (total, range) => {
    //展示总页数等文字说明信息的
    return (
        "共" +
        total +
        "条记录,当前第" +
        pagination.current +
        "页" +
        (range[1] - range[0] + 1) +
        "条记录"
    );
  },
});

const getPage2 = (pageNum = 1) => {
  selectTime.pageNum = pageNum;
  selectTime.pageSize = pagination.pageSize;
  selectTime.day = freeTimeNeedsTime.value.format("YYYY-MM-DD");

  post("/assister/consultChange/getFreeTime", selectTime).then((res) => {
    page2.value = res.data; //将服务端传来的分页数据赋值予page

    //更新分页引擎
    pagination2.current = page2.value.current;
    pagination2.pageSize = page2.value.pageSize;
    pagination2.total = page2.value.total;
  });
};

const handleTableChange2 = (pagi, filters, sorter, {currentDataSource}) => {
  pagination2.current = pagi.current;
  pagination2.pageSize = pagi.pageSize;
  pagination2.total = pagi.total;

  getPage2(pagination2.current);
};

const getTimeInfo = () => {
  getPage2(1);
};

const changeTimeInfo = (record) => {
  updateTimeInfo.place_name = selectTime.place_name;
  updateTimeInfo.day = selectTime.day;
  updateTimeInfo.time_id = record.time_id;
};
///////////////////////////////////////////

const visible = ref(false);

const freeTimeNeedsTime = ref("");

const openPermitExtra = (record) => {
  freeTimeNeedsTime.value = "";
  selectTime.day = "";
  selectTime.place_name = "";
  page2.value = {};
  visible.value = true;
  // day.value = record.day;
  updateTimeInfo.visit_id = record.visit_id;
  updateTimeInfo.apply_id = record.apply_id;
};

const temp = ref("");

const day = ref();

const disabledDate = (current) => {
  return current && current < dayjs().endOf("day");
};
////////////////////////////////////////////
const permitBody = reactive({
  visit_id: "",
});

const rejectBody = reactive({
  visit_id: "",
  apply_id: ""
});
////////////////////////////////////////////
const insertTimeInfo = () => {
  post("/admin/extraManage/permitExtra", updateTimeInfo).then((res) => {
    if (res.success) {
      message.success(res.message);
      visible.value = false;
      getPage(1);
      updateTimeInfo.visit_id = "";
      updateTimeInfo.place_name = "";
      updateTimeInfo.day = "";
      updateTimeInfo.time_id = "";
      updateTimeInfo.apply_id = "";
    } else {
      message.error(res.message);
      updateTimeInfo.visit_id = "";
      updateTimeInfo.place_name = "";
      updateTimeInfo.day = "";
      updateTimeInfo.time_id = "";
      updateTimeInfo.apply_id = "";
    }
  });
};
const rejectExtra = (record) => {
  rejectBody.visit_id = record.visit_id;
  rejectBody.apply_id = record.apply_id;
  post("/admin/extraManage/rejectExtra", rejectBody).then((res) => {
    if (res.success) {
      message.success(res.message);
      getPage(1);
      rejectBody.visit_id = "";
      rejectBody.apply_id = "";
    } else {
      message.error(res.message);
      getPage(1);
      rejectBody.visit_id = "";
      rejectBody.apply_id = "";
    }
  });
};
</script>

<style>
</style>

